package com.ct.gd.passport.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import com.ct.gd.passport.R;
import com.ct.gd.passport.common.BaseActivity;
import com.ct.gd.passport.common.constants.UmengVar;
import com.ct.gd.passport.domain.Helper;
import com.ct.gd.passport.view.discount.DiscountActivity;
import com.ct.gd.passport.view.exhibition.ExhibitionActivity;
import com.ct.gd.passport.view.ticket.TicketBookingActivity;
import com.ct.gd.passport.view.travel.ExchangeRateInfoActivity;
import com.ct.gd.passport.view.travel.MapTrafficActivity;
import com.ct.gd.passport.view.weather.WeatherServiceActivity;
import com.ct.gd.passport.view.widget.CustomHeaderFragment;
import com.umeng.analytics.MobclickAgent;
import com.up91.common.android.view.adapter.SimpleListAdapter;

import java.util.ArrayList;
import java.util.List;

public class TravelAssistActivity extends BaseActivity implements
		OnItemClickListener {

	private static final String TAG_BANK_TYPE_FRAGMENT = "TAG_BANK_TYPE_FRAGMENT";
	private List<Pair<Integer, Integer>> mItemInfos = new ArrayList<Pair<Integer, Integer>>();
	private CustomHeaderFragment mFgHeader;
	// private LocationFragment mFgLocation;
	// private GridView mGVEntries;
	private GridView mLVEntries;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.travel_assist);
	}

	@Override
	protected void initViews() {
		mFgHeader = (CustomHeaderFragment) getSupportFragmentManager()
				.findFragmentById(R.id.header);
		mFgHeader.setCenterText(R.string.travelAssist);

		// init bank type
		// mFgLocation = new LocationFragment();
		// mFgMan.beginTransaction().add(mFgLocation,
		// TAG_BANK_TYPE_FRAGMENT).commit();

		ArrayAdapter spAdapter = ArrayAdapter.createFromResource(this,
				R.array.travelAssist_switch_content,
				android.R.layout.simple_spinner_item);
		spAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// mSprArea.setAdapter(spAdapter);
		// mSprArea.setOnItemSelectedListener(new OnItemSelectedListener() {
		//
		// @Override
		// public void onItemSelected(AdapterView<?> arg0, View arg1, int
		// position, long id) {
		// Config.currentArea = Config.AREAS[position];
		// }
		//
		// @Override
		// public void onNothingSelected(AdapterView<?> arg0) {
		//
		// }
		// });
		mLVEntries = (GridView) findViewById(R.id.gv_travel_entries);

		//屏蔽关口
//		mItemInfos.add(new Pair<Integer, Integer>(R.drawable.ic_travel_port,
//				R.string.travelAssist_port));
		mItemInfos.add(new Pair<Integer, Integer>(R.drawable.ic_travel_map,
				R.string.travelAssist_map));
		mItemInfos.add(new Pair<Integer, Integer>(
				R.drawable.ic_travel_exhibition,
				R.string.travelAssist_exhibition));
		mItemInfos.add(new Pair<Integer, Integer>(
				R.drawable.ic_travel_discount, R.string.travelAssist_discount));
		mItemInfos.add(new Pair<Integer, Integer>(R.drawable.ic_travel_weather,
				R.string.travelAssist_weather));
		mItemInfos.add(new Pair<Integer, Integer>(R.drawable.ic_travel_roe,
				R.string.travelAssist_exchange));
        mItemInfos.add(new Pair<Integer, Integer>(R.drawable.ic_travel_traffic,
                R.string.travelAssist_ticket));

        TravelListAdapter adapter = new TravelListAdapter(this, mItemInfos);
		mLVEntries.setAdapter(adapter);
		mLVEntries.setOnItemClickListener(this);
		// mGVEntries = (GridView) findViewById(R.id.gv_home_entries);
		// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		// R.layout.main_entries_item, R.id.tv_main_entries_title,
		// getResources().getStringArray(R.array.travelAssist_entries_title));
		// mGVEntries.setAdapter(adapter);
		// mGVEntries.setOnItemClickListener(this);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		if (Helper.hasAgreeUserProtocol(this) == false) {
			startActivityForResult(
					new Intent(this, UserProtocolActivity.class), 0);
		}
		super.onAfterCreate(savedInstanceStat);
	}

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Class<? extends Activity> target = null;
		switch (position) {
	      //屏蔽关口
//		case 0:
//			// ic_travel_port
//			MobclickAgent.onEvent(this, UmengVar.TRAVEL_PORT);
//			 target = TravelPortInfoActivity.class;
//			break;
		case 0:
			// ic_travel_map
			MobclickAgent.onEvent(this, UmengVar.TRAVEL_TRAFFIC);
			target = MapTrafficActivity.class;
			break;
		case 1:
			// ic_travel_exhibition
			MobclickAgent.onEvent(this, UmengVar.TRAVEL_EXHIBITION);
			target =ExhibitionActivity.class;
			break;
		case 2:
			// ic_travel_discount
			MobclickAgent.onEvent(this, UmengVar.TRAVEL_DISCOUNT);
			target = DiscountActivity.class;
			break;
		case 3:
			// ic_travel_weather
			MobclickAgent.onEvent(this, UmengVar.TRAVEL_WEATHER);
			target = WeatherServiceActivity.class;
			break;
			
		case 4:
            // ic_travel_roe
            MobclickAgent.onEvent(this, UmengVar.TRAVEL_EXCHANGE);
            target = ExchangeRateInfoActivity.class;
            break;
        //屏蔽交通出行
		case 5:
			// travelAssist_ticket
			MobclickAgent.onEvent(this, UmengVar.TRAVEL_MAP);
			target = TicketBookingActivity.class;
			break;
        }
		if (null != target)
			startActivity(new Intent(this, target));
	}

	private static class TravelListAdapter extends
			SimpleListAdapter<Pair<Integer, Integer>> {
		public TravelListAdapter(Context context,
				List<Pair<Integer, Integer>> data) {
			super(context, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = mInflater.inflate(R.layout.main_entries_item, parent,
					false);
			TextView tv = (TextView) convertView
					.findViewById(R.id.tv_main_entry_item);
			tv.setCompoundDrawablesWithIntrinsicBounds(0,
					getData().get(position).first, 0, 0);
			tv.setText(getData().get(position).second);
			return convertView;
		}
	}
	// class TravelListAdapter extends SimpleListAdapter<String> {
	//
	// int[] icons;
	// static final int ICON_DEFAULT = R.drawable.icon_info;
	//
	// private class ViewHolder {
	// TextView tvTitle;
	// }
	//
	// public TravelListAdapter(Context context, String[] data, int[] icons) {
	// super(context, data);
	// this.icons = icons;
	// }
	//
	// public View getView(int position, View convertView, ViewGroup parent) {
	// ViewHolder holder;
	// if (convertView == null) {
	// convertView = mInflater.inflate(R.layout.assist_list_item, null);
	// holder = new ViewHolder();
	// holder.tvTitle = (TextView)
	// convertView.findViewById(R.id.tv_assist_title);
	// convertView.setTag(holder);
	// } else {
	// holder = (ViewHolder) convertView.getTag();
	// }
	// holder.tvTitle.setText(getData().get(position));
	// int resIcon = ICON_DEFAULT;
	// if (icons != null && position < icons.length && icons[position] != 0) {
	// resIcon = icons[position];
	// }
	// holder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(resIcon, 0, 0, 0);
	// holder.tvTitle.setCompoundDrawablePadding(10);
	// return convertView;
	// }
	//
	// }

	// @Override
	// protected void onAfterCreate(Bundle savedInstanceStat) {
	// mFgLocation.registerOnLocationsLoadedListener(this);
	// }
	//
	// @Override
	// public void onLocationsLoaded(List<LocationFragment.Location> bankTypes)
	// {
	// // 加载BankType结束后BankTypeFragment的View才确定(一些因素比如:BankType只有一个则不显示切换按钮)
	// mFgHeader.setRightView(mFgLocation.getView());
	// }
}
